package com.zwface.demo.greendao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.zwface.demo.model.db.JoinUserWithCourse;

import com.zwface.demo.model.db.UserInfo;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "USER_INFO".
*/
public class UserInfoDao extends AbstractDao<UserInfo, Long> {

    public static final String TABLENAME = "USER_INFO";

    /**
     * Properties of entity UserInfo.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property StuNo = new Property(1, String.class, "stuNo", false, "STU_NO");
        public final static Property Name = new Property(2, String.class, "name", false, "NAME");
        public final static Property Password = new Property(3, String.class, "password", false, "PASSWORD");
        public final static Property Type = new Property(4, int.class, "type", false, "TYPE");
        public final static Property Face = new Property(5, String.class, "face", false, "FACE");
    }

    private DaoSession daoSession;

    private Query<UserInfo> course_UserInfosQuery;

    public UserInfoDao(DaoConfig config) {
        super(config);
    }
    
    public UserInfoDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"USER_INFO\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"STU_NO\" TEXT," + // 1: stuNo
                "\"NAME\" TEXT NOT NULL ," + // 2: name
                "\"PASSWORD\" TEXT," + // 3: password
                "\"TYPE\" INTEGER NOT NULL ," + // 4: type
                "\"FACE\" TEXT);"); // 5: face
        // Add Indexes
        db.execSQL("CREATE UNIQUE INDEX " + constraint + "IDX_USER_INFO_STU_NO ON \"USER_INFO\"" +
                " (\"STU_NO\" ASC);");
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"USER_INFO\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, UserInfo entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String stuNo = entity.getStuNo();
        if (stuNo != null) {
            stmt.bindString(2, stuNo);
        }
        stmt.bindString(3, entity.getName());
 
        String password = entity.getPassword();
        if (password != null) {
            stmt.bindString(4, password);
        }
        stmt.bindLong(5, entity.getType());
 
        String face = entity.getFace();
        if (face != null) {
            stmt.bindString(6, face);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, UserInfo entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String stuNo = entity.getStuNo();
        if (stuNo != null) {
            stmt.bindString(2, stuNo);
        }
        stmt.bindString(3, entity.getName());
 
        String password = entity.getPassword();
        if (password != null) {
            stmt.bindString(4, password);
        }
        stmt.bindLong(5, entity.getType());
 
        String face = entity.getFace();
        if (face != null) {
            stmt.bindString(6, face);
        }
    }

    @Override
    protected final void attachEntity(UserInfo entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public UserInfo readEntity(Cursor cursor, int offset) {
        UserInfo entity = new UserInfo( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // stuNo
            cursor.getString(offset + 2), // name
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // password
            cursor.getInt(offset + 4), // type
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5) // face
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, UserInfo entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setStuNo(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setName(cursor.getString(offset + 2));
        entity.setPassword(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setType(cursor.getInt(offset + 4));
        entity.setFace(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(UserInfo entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(UserInfo entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(UserInfo entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "userInfos" to-many relationship of Course. */
    public List<UserInfo> _queryCourse_UserInfos(Long cId) {
        synchronized (this) {
            if (course_UserInfosQuery == null) {
                QueryBuilder<UserInfo> queryBuilder = queryBuilder();
                queryBuilder.join(JoinUserWithCourse.class, JoinUserWithCourseDao.Properties.UId)
                    .where(JoinUserWithCourseDao.Properties.CId.eq(cId));
                course_UserInfosQuery = queryBuilder.build();
            }
        }
        Query<UserInfo> query = course_UserInfosQuery.forCurrentThread();
        query.setParameter(0, cId);
        return query.list();
    }

}
